package com.lgy.controller;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.lgy.model.User;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.common.SolrDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

@RestController
public class TestController {
    @Autowired
    private SolrClient solrClient;

    @RequestMapping("/addUser")
    public void addUser() throws IOException, SolrServerException {
        User user = new User();
        user.setId("11111");
        user.setName("王强21");
        user.setAddress("北京市");
        user.setSex("女");
        user.setHost(456752);
        solrClient.addBean(user);
        solrClient.commit();
    }

    //根据di查询
    @RequestMapping("/getByIdFromSolr/{id}")
    public User getByIdFromSolr(@PathVariable("id") String id) throws IOException, SolrServerException {
        SolrDocument solrDocument = solrClient.getById(id);
        JSONObject data = JSONUtil.parseObj(solrDocument);
        System.out.println(data.toString());
        return null;
    }

    @RequestMapping("/updateUser")
    public void updateUser(String userId,String userName) throws IOException, SolrServerException {
        User user = new User();
        user.setId("456788");
        user.setName("王强3");
        user.setAddress("北京市");
        user.setSex("女");
        user.setHost(456752);
        solrClient.addBean(user);
        solrClient.commit();
    }

}
